<?php
require_once("../../config/config.inc.php");
require_once($CFG_REAL_CLASSES."/json.class.php");
$json = new Services_JSON();

$sEPID   = $SEC->sCorpCD;
$sAction = fn_getParam("strAction","");


if ($sAction=="GET_COUNTRY_LIST") {
    $sql = "SELECT CODE,NAME FROM $TABLE_COUNTRY ORDER BY CODE";
    $rset= $DB->query($sql);
    while ($row = $DB->fetchRow($rset)) {
        $aCountry[] = array('sValue'=>$row["CODE"] ,'sText'=>$row["NAME"]);
    }
    $DB->freeResult($rset);
    echo $json->encode(array('success'=>true,'data'=>$aCountry));
    unset($aCountry);
    return;
} else 
if ($sAction == "GET_PROVINCE_LIST") { 
    $sCode = fn_getParam("strCode","");
    $sql = "SELECT ID,NAME,PROVINCECITY FROM $TABLE_PROVINCE WHERE COUNTRYCODE='".$sCode."'";
    $rset= $DB->query($sql);
    while ($row = $DB->fetchRow($rset)) {
        $aProvince[] = array('sValue'=>$row["ID"] ,'sText'=>$row["NAME"]);
    }
    $DB->freeResult($rset);

    echo $json->encode(array('success'=>true,'data'=>$aProvince));
    unset($aProvince);
    return;
} else 
if ($sAction=="GET_CITY_LIST") {
    $sProvinceID = fn_getParam("strProvinceID","");
    $sql = "SELECT ID,CODE,NAME FROM $TABLE_CITY WHERE PROVINCEID='".$sProvinceID."'";
    $rset= $DB->query($sql);
    while ($row = $DB->fetchRow($rset)) {
        $aCity[] = array('sValue'=>$row["ID"] ,'sText'=>$row["NAME"]);
    }
    $DB->freeResult($rset);
    echo $json->encode(array('success'=>true,'data'=>$aCity));
    unset($aCity);
    return;
} else 
if ($sAction=="GET_COUNTY_LIST") {
    $sCityID = fn_getParam("strCityID","");
    $sql = "SELECT ID,NAME,POSTCODE FROM $TABLE_COUNTY WHERE CITYID='".$sCityID."'";
    $rset= $DB->query($sql);
    while ($row = $DB->fetchRow($rset)) {
        $aCounty[] = array('sValue'=>$row["ID"] ,'sText'=>$row["NAME"]);
    }
    $DB->freeResult($rset);
    echo $json->encode(array('success'=>true,'data'=>$aCounty));
    unset($aCounty);
    return;
} else 
if ($sAction=='UserImage') {
    $node = isset($_REQUEST['node']) ? $_REQUEST['node']:".";
    if (strpos($node, '..') !== false) {
        return false;
    }
    $newPath = $SEC->USER_REAL_PATH.'/'.$node;
    if (!is_dir($newPath)) return false;
    
    $nodes = array();
    $d = dir($newPath);
    while ($f = $d->read()){
        if ($f == '.' || $f == '..' || substr($f, 0, 1) == '.') continue;
        if (is_dir($newPath.'/'.$f)) {
            $nodes[] = array('text'=>$f, 'id'=>$node.'/'.$f, 'qtip'=>'', 'cls'=>'folder');
        } else if (preg_match('/\.(jpg|jpeg|gif|png)$/', $f)) {
            $lastmod = date('M j, Y, g:i a',filemtime($newPath.'/'.$f));
            $qtip = '最后更新时间: '.$lastmod;
            $nodes[] = array('text'=>$f, 'id'=>$node.'/'.$f, 'leaf'=>true, 'qtip'=>$qtip, 'cls'=>'file');
        }
    }
    $d->close();
    echo $json->encode($nodes);
} else
if ($sAction=="EP_VIEW") {
    ////get enterprise info
    $sql = "SELECT ID,VIPID,EPNAME,PARENTNAME,EPTYPE,INTRO,COUNTRY_ID,PROVINCE_ID,CITY_ID,COUNTY_ID,";
    $sql.= "LOGOIMG,LOGOIMGDTL,PASSIMG,PASSIMGDTL,EMPIMG,EMPIMGDTL,";
    $sql.= "MANAGER,CONTACTER,ADDRESS,POSTCODE,TELEPHONE,MOBILEPHONE,FAX,EMAIL,WEBSITE,";
    $sql.= "REGDATE,MODIFYDATE,LOCKSTATUS,HITS,REMARK ";
    $sql.= "FROM $TABLE_ENTERPRISE ";
    $sql.= "WHERE ID='".$SEC->sCorpCD."'";
    $stmt = $DB->query($sql);
    if ($row = $DB->fetchArray($stmt)) {
        $aEP["strEPID"]       = $row["ID"];
        $aEP["strVipID"]      = $row["VIPID"];
        $aEP["strEPName"]     = $row["EPNAME"];
        $aEP["strParentName"] = $row["PARENTNAME"];
        $aEP["strIntro"]      = $row["INTRO"];
        $aEP["strEPType"]     = $row["EPTYPE"];
        $aEP["strContacter"]  = $row["CONTACTER"];
        $aEP["strAddress"]    = $row["ADDRESS"];
        $aEP["strPostcode"]   = $row["POSTCODE"];
        $aEP["strTelephone"]  = $row["TELEPHONE"];
        $aEP["strMobilephone"]= $row["MOBILEPHONE"];
        $aEP["strFax"]        = $row["FAX"];
        $aEP["strEMail"]      = $row["EMAIL"];
        $aEP["strWebsite"]    = $row["WEBSITE"];
        $aEP["strRegDate"]    = $row["REGDATE"];
        $aEP["strModifyDate"] = $row["MODIFYDATE"];
        $aEP["sCountryID"]    = $row["COUNTRY_ID"];
        $aEP["sProvinceID"]   = $row["PROVINCE_ID"];
        $aEP["sCityID"]       = $row["CITY_ID"];
        $aEP["sCountyID"]     = $row["COUNTY_ID"];
        $aEP["strLogoImg"]    = $row["LOGOIMG"];
        $aEP["strLogoImgDtl"] = $row["LOGOIMGDTL"];
        $aEP["strPassImg"]    = $row["PASSIMG"];
        $aEP["strPassImgDtl"] = $row["PASSIMGDTL"];
        $aEP["strEmpImg"]     = $row["EMPIMG"];
        $aEP["strEmpImgDtl"]  = $row["EMPIMGDTL"];
    }
    $DB->freeResult($stmt);

    echo $json->encode(array('success'=>true,'total'=>"1",'data'=>$aEP));
    unset($aEP);
    return;
}
else
if ($sAction == ACTION_SAVE) { //send enterprise message
    $sEPName       = fn_getParam("strEPName","");
    $sParentName   = fn_getParam("strParentName","");
    $sManager      = fn_getParam("strManager","");
    $sIntro        = fn_getParam("strIntro","");
    $sEPType       = fn_getParam("strEPType","");
    $sCountryID    = fn_getParam("strCountryID","0");
    $sProvinceID   = fn_getParam("strProvinceID","0");
    $sCityID       = fn_getParam("strCityID","0");
    $sCountyID     = fn_getParam("strCountyID","0");
    $sContacter    = fn_getParam("strContacter","");
    $sAddress      = fn_getParam("strAddress","");
    $sPostcode     = fn_getParam("strPostcode","");
    $sTelephone    = fn_getParam("strTelephone","");
    $sMobilephone  = fn_getParam("strMobilephone","");
    $sFax          = fn_getParam("strFax","");
    $sEMail        = fn_getParam("strEMail","");
    $sWebsite      = fn_getParam("strWebsite","");
    $sRemark       = fn_getParam("strRemark","");
    $sLogoImg      = fn_getParam("strLogoImg","");
    $sLogoImgDtl   = fn_getParam("strLogoImgDtl","");
    $sPassImg      = fn_getParam("strPassImg","");
    $sPassImgDtl   = fn_getParam("strPassImgDtl","");
    $sEmpImg       = fn_getParam("strEmpImg","");
    $sEmpImgDtl    = fn_getParam("strEmpImgDtl","");

    $sql = "SELECT COUNT(ID) FROM $TABLE_ENTERPRISE ";
    $sql.= "WHERE EPNAME='".$sEPName."' AND ID<>'".$SEC->sCorpCD."'";
    if ($DB->getFirstField($sql) > 0) {
        $sErrMesg = "单位名称  ".$sEPName." 已经存在，不能重复!";
    } else {
        $sql = "UPDATE $TABLE_ENTERPRISE SET ";
        $sql.= "EPNAME='".$sEPName."',PARENTNAME='".$sParentName."',INTRO='".$sIntro."',EPTYPE='".$sEPType."',";
        $sql.= "COUNTRY_ID='".$sCountryID."',PROVINCE_ID='".$sProvinceID."',CITY_ID='".$sCityID;
        $sql.= "',COUNTY_ID='".$sCountyID."',CONTACTER='".$sContacter."',ADDRESS='".$sAddress."',";
        $sql.= "POSTCODE='".$sPostcode."',TELEPHONE='".$sTelephone."',MOBILEPHONE='".$sMobilephone."',";
        $sql.= "FAX='".$sFax."',EMAIL='".$sEMail."',WEBSITE='".$sWebsite."',MODIFYDATE=now(),";
        $sql.= "LOGOIMG='".$sLogoImg."',LOGOIMGDTL='".$sLogoImgDtl."',PASSIMG='".$sPassImg."',";
        $sql.= "PASSIMGDTL='".$sPassImgDtl."',EMPIMG='".$sEmpImg."',EMPIMGDTL='".$sEmpImgDtl."' ";
        $sql.= "WHERE ID='".$SEC->sCorpCD."'";
        $DB->query($sql);
        $sErrMesg = "单位信息已经成功更改";
    }
    echo $json->encode(array('success'=>true,'msg'=>$sErrMesg));
}

?>